﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class NeedMaterialShow : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             if (!this.IsPostBack)
            {
                if (Request.QueryString["SalesOrderID"] != null)
                {
                    if (mk.IsNumber(Request.QueryString["SalesOrderID"].ToString()))
                    {
                        //用一个隐藏Label保存传递过来的ID
                        this.lblSalesOrderID.Text = Request.QueryString["SalesOrderID"].ToString();
                    }
                    else
                    {
                       mk.Alert("参数错误！");
                    }
                }
                else
                {
                   mk.Alert("参数错误！");
                }
                //设置经办人
             Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();

                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                 this.lblRootDepartmentID.Text = bllDept.Where(j=>j.ClassID==(int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
            var bllOrder =mk.l.Sales_SalesOrderHeader.ToList();
                       Sales_SalesOrderHeader mOrder = bllOrder.Where(h=>h.SalesOrderID==int.Parse(this.lblSalesOrderID.Text)).FirstOrDefault();
                if (int.Parse(this.lblRootDepartmentID.Text) == mOrder.RootDepartmentID)
                {
                    //绑定指定订单的用料单列表
                    this.BindNeedMaterialHeader();
                }
                else
                {
                   mk.Alert("只能查看本店的订单！");
                }

                //显示订单概况
                //this.ShowSalesOrderHeaderInfo();
                //this.BindManufacturerID();
                //设置经办人信息
                //UART.Accounts.Bus.User currentUser = (UART.Accounts.Bus.User)(Session["UserInfo"]);
                //this.lblManagerTrueName.Text = currentUser.TrueName;
                //this.lblManagerUserName.Text = currentUser.UserName;
                //给日期输入框添加只读属性，不能在属性里直接设置，不然会取不到值
                //this.txtDateOrder.Attributes["readonly"] = "true";
                //this.txtDateOrder.Text = System.DateTime.Now.ToString("yyyy-MM-dd");
            }
        }
        private void BindNeedMaterialHeader()
        {
           var bll = mk.l.Sales_NeedMaterialHeader.ToList();
            //需要把最后的逗号去掉
         var ds = bll.Where(h=>h.SalesOrderID.ToString()== this.lblSalesOrderID.Text).ToList();
            this.myDataList.DataSource = ds;
            this.myDataList.DataBind();
        }

        /// <summary>
        /// Handles the ItemDataBound event of the myDataList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.DataListItemEventArgs"/> instance containing the event data.</param>
        protected void myDataList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //绑定用料单明细列表
                GridView myGridViewNeedMaterialDetail = (GridView)e.Item.FindControl("myGridViewNeedMaterialDetail");
                if (myGridViewNeedMaterialDetail != null)
                {
                    this.BindNeedMaterialDetail(myGridViewNeedMaterialDetail, this.myDataList.DataKeys[e.Item.ItemIndex].ToString());
                }
                //显示订货单概况信息
                Label lblManager = (Label)e.Item.FindControl("lblManager");
                Label lblDateOrder = (Label)e.Item.FindControl("lblDateOrder");
                Label lblStatusSubmit = (Label)e.Item.FindControl("lblStatusSubmit");
                Label lblStatusPurchaseMaterial = (Label)e.Item.FindControl("lblStatusPurchaseMaterial");
     var bllHeader = mk.l.Sales_NeedMaterialHeader.ToList();
               Sales_NeedMaterialHeader mHeader = bllHeader.Where(h=>h.NeedMaterialID==int.Parse(this.myDataList.DataKeys[e.Item.ItemIndex].ToString())).FirstOrDefault();
                if (mHeader != null)
                {
                    
                    //显示经办人姓名
                    var bllUser = mk.l.Accounts_Users.ToList();
                  Accounts_Users mUser = bllUser.Where(h=>h.UserName==mHeader.Manager).FirstOrDefault();
                    lblManager.Text = mUser.TrueName;
                    //显示用料单日期
                    lblDateOrder.Text = mHeader.DateOrder.ToString();
                    //显示提交状态
                    if (mHeader.StatusSubmit == "1")
                    {
                        lblStatusSubmit.Text = "未提交";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        lblStatusSubmit.Text = "已提交";
                        lblStatusSubmit.ForeColor = System.Drawing.Color.Green;
                    }
                    //显示订货状态
                    if (mHeader.StatusPurchaseMaterial == "1")
                    {
                        lblStatusPurchaseMaterial.Text = "未订货";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Red;
                    }
                    else
                    {
                        lblStatusPurchaseMaterial.Text = "已订货";
                        lblStatusPurchaseMaterial.ForeColor = System.Drawing.Color.Green;
                    }
                }

            }
        }

        /// <summary>
        /// Binds the need material detail.
        /// </summary>
        /// <param name="myGridViewNeedMaterialDetail">My grid view need material detail.</param>
        /// <param name="NeedMaterialID">The need material ID.</param>
        private void BindNeedMaterialDetail(GridView myGridViewNeedMaterialDetail, string NeedMaterialID)
        {
          var bll = mk.l.Sales_NeedMaterialDetailandSales_Material.ToList();
            var ds = bll.Where(y=>y.NeedMaterialID.ToString()==NeedMaterialID);
            myGridViewNeedMaterialDetail.DataSource = ds;
            myGridViewNeedMaterialDetail.DataBind();
        }

        /// <summary>
        /// Handles the RowDataBound event of the myGridViewNeedMaterialDetail control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
        protected void myGridViewNeedMaterialDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView myGridViewNeedMaterialDetail = (GridView)(sender);
                Label lblManufacturerName = (Label)(e.Row.FindControl("lblManufacturerName"));
               var bllManufacturer =mk.l.Sales_Manufacturer.ToList();
            Sales_Manufacturer mManufacturer = bllManufacturer.Where(h=>h.ManufacturerID==int.Parse(myGridViewNeedMaterialDetail.DataKeys[e.Row.RowIndex].Values["ManufacturerID"].ToString())).FirstOrDefault();
                lblManufacturerName.Text = mManufacturer.ManufacturerName;
            }
        }
    }
}